package com.efounder.form.service;

import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.json.JSONUtil;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.base.DataComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.builder.IScriptObject;
import com.efounder.message.db.MessageDBHelper;
import com.efounder.message.db.MessageDBManager;
import com.efounder.message.struct.IMStruct002;
import com.efounder.service.IService;
import com.efounder.util.AppContext;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import static com.efounder.builder.base.json.JSONUtil.JSON2Object;

/**
 * Created by Javor on 16/9/20.
 */

public class JSONFormModelDBLoadService extends DataComponent implements IService, IComponent,IScriptObject {
    private String messageID;
    private String key;

    private Map scriptObject = new HashMap();
    private Map scriptContext = new HashMap();

    @Override
    public Object executeService(JParamObject po, Object dao, Object co, Object userObject,
                                 Object ao) {
//        MessageDBHelper sqlHelper = new MessageDBHelper(AppContext.getInstance());
//        MessageDBManager dbManager = new MessageDBManager(sqlHelper);
        MessageDBManager dbManager = MessageDBManager.getInstance();

        IMStruct002 imStruct002 = dbManager.queryForID(messageID);
        String json = null;
        try {
            if (imStruct002 != null) {
                json = new String(imStruct002.getBody(), "UTF-8");
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        EFRowSet rowSet = JSON2RowSet(json, key);
        return rowSet;


    }

    public String getMessageID() {
        return messageID;
    }

    public void setMessageID(String messageID) {
        this.messageID = messageID;
    }

    private String id;

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public void creationComplete() {

    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public EFRowSet JSON2RowSet(String jsonStr, String key) {
        EFRowSet rowSet = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            String json = jsonObject.getString(key);
            rowSet = JSONUtil.JSON2RowSet(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return rowSet;
    }

    @Override
    public Map getScriptObject() {
        return scriptObject;
    }

    @Override
    public void setScriptObject(Map scriptObject) {
        this.scriptObject = scriptObject;
    }

    @Override
    public String getEventScript(String key) {
        return (String) scriptObject.get(key);
    }

    @Override
    public void setEventScript(String key, String script) {
        scriptObject.put(key, script);
    }

    @Override
    public Map getScriptContext() {
        return scriptContext;
    }

    @Override
    public void setScriptContext(Map context) {
        scriptContext = context;
    }
}
